package top.scsoul.voes.web.controller.system;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.scsoul.voes.common.core.domain.AjaxResult;
import top.scsoul.voes.common.core.domain.model.IdsVo;
import top.scsoul.voes.common.core.domain.model.RoleSearchVo;
import top.scsoul.voes.common.core.domain.model.RoleVo;
import top.scsoul.voes.common.core.domain.entity.Role;
import top.scsoul.voes.system.service.RoleService;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

@CrossOrigin(allowedHeaders = {"*"})
@RestController
@RequestMapping("/api/role")
public class RoleController {
    @Autowired
    RoleService roleService;

    @RequestMapping("/rolesDict")
    public AjaxResult roles() {
        List<Role> roles = roleService.queryAll(null);
        return AjaxResult.success(roles);
    }

    @RequestMapping("/roles")
    public AjaxResult rolesPage(
            RoleSearchVo roleSearchVo,
            @RequestParam(defaultValue = "1") int currentPage,
            @RequestParam(defaultValue = "10") int pageSize) {
        PageInfo<Role> rolePageInfo = roleService.queryAll(roleSearchVo, currentPage, pageSize);
        return AjaxResult.success(rolePageInfo);
    }

    @RequestMapping("/get/{id}")
    public AjaxResult getRoleById(@PathVariable("id") Integer id) {
        Role role = roleService.queryById(id);
        return AjaxResult.success(role);
    }

    @RequestMapping("/delete")
    public AjaxResult deleteById(Integer id) {
        return AjaxResult.success(roleService.deleteById(id));
    }

    @RequestMapping("/add")
    public AjaxResult add(@Valid RoleVo roleVo) {
        Role role = roleService.insert(roleVo);
        return AjaxResult.success(role);
    }

    @RequestMapping("/update")
    public AjaxResult update(@Valid RoleVo roleVo) {
        Role role = roleService.update(roleVo);
        return AjaxResult.success(role);
    }


    @RequestMapping("/export")
    public void export(@Valid IdsVo idsVo, HttpServletResponse response) throws IOException {
        roleService.exportRole(idsVo.getIds(), response);
    }
}
